//
//  ViewController.swift
//  20160227-swift闭包的练习
//
//  Created by 王柯佳 on 16/2/27.
//  Copyright © 2016年 wkjsos. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        let rect = CGRect(x: 0, y: 20, width: view.bounds.width, height: 40)
        
        let scrollView = creatScrollView(rect, numberOfLabel: { () -> Int in
            return 16
            }) { (indexOfSubLabel) -> UILabel in
                let label = UILabel()
                
                label.text = "wkj \(indexOfSubLabel)"
                
                label.sizeToFit()
                
                return label
        }
        
        view.addSubview(scrollView)
    }
    
    
    /*
        1. 实现类似网易新闻头部标签滚动条的scrollView
    
        2. 声明一个方法，用来初始化scrollView
    
        3.
    
    */
    
    
    func creatScrollView (frame : CGRect , numberOfLabel: () -> Int , subLabel : (indexOfSubLabel : Int) -> UILabel) -> UIScrollView{
        
        let scrollView = UIScrollView(frame: frame)
        
        scrollView.backgroundColor = UIColor.greenColor()
        
        let number = numberOfLabel();
        
        let labelMargin : CGFloat = 10
        
        var x = labelMargin
        
        for i in 0..<number{
            
            let label = subLabel(indexOfSubLabel: i)
            
            label.frame = CGRect(x: x, y: 0, width: label.bounds.width, height: scrollView.bounds.height)
            
            x += label.bounds.width
            
            scrollView.addSubview(label)
        }
        
        scrollView.contentSize = CGSize(width: x, height: scrollView.bounds.height)
        
        return scrollView;
    }
    
    

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }


}

